import {
  login,
  logout,
  userinfo,
  all,
  allId,
  allScheduleId,
  add,
  update,
  del
} from '@/api/user'
import {
  load
} from '@/api/menu'
import {
  getToken,
  setToken,
  removeToken
} from '@/utils/auth'
import {
  resetRouter
} from '@/router'

const getDefaultState = () => {
  return {
    token: getToken(),
    name: '',
    avatar: '',
    roleId: '',
    menus: []
  }
}

const state = getDefaultState()

const mutations = {
  RESET_STATE: (state) => {
    Object.assign(state, getDefaultState())
  },
  SET_TOKEN: (state, token) => {
    state.token = token
  },
  SET_NAME: (state, name) => {
    state.name = name
  },
  SET_AVATAR: (state, avatar) => {
    state.avatar = avatar
  },
  SET_MENUS: (state, menus) => {
    state.menus = menus
  }
}

const actions = {
  // user login
  login({
    commit
  }, userInfo) {
    const {
      username,
      password
    } = userInfo
    return new Promise((resolve, reject) => {
      login({
        username: username.trim(),
        password: password
      }).then(response => {
        const {
          data
        } = response
        localStorage.setItem('hospitalId', data.hospitalId)
        localStorage.setItem('userId', data.userId)
        commit('SET_TOKEN', data.authorization)
        setToken(data.authorization)
        resolve()
      }).catch(error => {
        reject(error)
      })
    })
  },

  // get user info
  userinfo({
    commit,
    state
  }) {
    return new Promise((resolve, reject) => {
      userinfo(state.token).then(response => {
        const {
          data
        } = response

        if (!data) {
          return reject('Verification failed, please Login again.')
        }

        const {
          name
        } = data

        commit('SET_NAME', name)
        // commit('SET_AVATAR', avatar)
        resolve(data)
      }).catch(error => {
        reject(error)
      })
    })
  },

  // user logout
  logout({
    commit,
    state
  }) {
    return new Promise((resolve, reject) => {
      logout(state.token).then(() => {
        removeToken() // must remove  token  first
        resetRouter()
        commit('RESET_STATE')
        resolve()
      }).catch(error => {
        reject(error)
      })
    })
  },

  // remove token
  resetToken({
    commit
  }) {
    return new Promise(resolve => {
      removeToken() // must remove  token  first
      commit('RESET_STATE')
      resolve()
    })
  },

  // user load
  load({
    commit
  }) {
    return new Promise((resolve, reject) => {
      load().then(response => {
        let menus = []

        menus = response.data
        menus.push({
          path: '*',
          redirect: '/404',
          hidden: true
        })
        commit('SET_MENUS', menus)

        resolve()
      }).catch(error => {
        reject(error)
      })
    })
  },

  all({
    commit
  }, userInfo) {
    const {
      obj,
      page,
      pageSize
    } = userInfo
    return new Promise((resolve, reject) => {
      all({
        obj: obj,
        page: page,
        pageSize: pageSize
      }).then(response => {
        resolve(response.data)
      }).catch(error => {
        reject(error)
      })
    })
  },

  allId({
    commit
  }, userInfo) {
    const {
      roleId,
      hospitalId,
      sectionId,
      name
    } = userInfo
    return new Promise((resolve, reject) => {
      allId({
        roleId: roleId,
        hospitalId: hospitalId,
        sectionId: sectionId,
        name: name,
      }).then(response => {
        resolve(response.data)
      }).catch(error => {
        reject(error)
      })
    })
  },

  allScheduleId({
    commit
  }, userInfo) {
    const {
      sectionId
    } = userInfo
    return new Promise((resolve, reject) => {
      allScheduleId({
        sectionId: sectionId
      }).then(response => {
        resolve(response.data)
      }).catch(error => {
        reject(error)
      })
    })
  },

  add({
    commit
  }, userInfo) {
    return new Promise((resolve, reject) => {
      add(userInfo).then(response => {
        resolve(response.data)
      }).catch(error => {
        reject(error)
      })
    })
  },

  update({
    commit
  }, {
    userId,
    name,
    username,
    password,
    roleId,
    hospitalId,
    sectionId
  } = userInfo) {
    return new Promise((resolve, reject) => {
      update({
        userId: userId,
        name: name,
        username: username,
        password: password,
        roleId: roleId,
        hospitalId: hospitalId,
        sectionId: sectionId
      }).then(response => {
        resolve(response.data)
      }).catch(error => {
        reject(error)
      })
    })
  },

  delete({
    commit
  }, ids) {
    return new Promise((resolve, reject) => {
      del(ids).then(response => {
        resolve()
      }).catch(error => {
        reject(error)
      })
    })
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
